using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;


namespace xingzhimeng
{
  ///<summary>
  ///
  ///</summary>
public class EnemyManager : MonoBehaviour
  {
    private static EnemyManager manager;
    public static EnemyManager insernt
    {
      get { return manager;}
      private set { manager = value; }
    }

    private List<int> currentEnemyID;//当前所以敌人的所在的方格
    private List<int> currentPointEnemy;//记录当前敌人的类型数组
    public Transform[] EnemyPoint;//敌人出生点
    public GameObject[] Enemys;
    public float[] pointMove;//位置偏移量
    
    

    private void Awake()
    {
      manager = this;
      currentEnemyID = new List<int>();
      currentPointEnemy = new List<int>();
    }

    private void Start()
    {
      StartCoroutine(QIYong());
    }
    //刷新新游戏
    public void UpGame()
    {
      StopAllCoroutines();
      DeleteEnemy();
      StartCoroutine(QIYong());
    }

    IEnumerator QIYong()
    {
      while (true)
      {
        float time = Random.Range(2f, 4f);
        yield return new WaitForSeconds(time);
        CreateEnemy();
      }
    }

    //敌人生成方法
    private void CreateEnemy()
    {
      int max = Random.Range(2, 5);//随机生成敌人数
      DeleteEnemy();//删除对象
      currentPointEnemy.Clear();
      while (currentEnemyID.Count<max)
      {
        int id = Random.Range(0, EnemyPoint.Length);
        if (!currentEnemyID.Contains(id))
        {
          currentEnemyID.Add(id);//记录位置编号
        }

      }
//创建对象
      for (int i = 0; i < currentEnemyID.Count; i++)
      {
        int EnemyID = Random.Range(0, Enemys.Length);
        currentPointEnemy.Add(EnemyID);
      GameObject obj=  GameObject.Instantiate(Enemys[EnemyID], EnemyPoint[currentEnemyID[i]].position+
         new Vector3(0,pointMove[EnemyID],0),
        Quaternion.identity);
      obj.transform.parent = EnemyPoint[currentEnemyID[i]];

      }
      
    }
//怪物死亡方法
    private void DeleteEnemy()
    {
      for (int i = 0; i < currentEnemyID.Count; i++)
      {
        if (EnemyPoint[currentEnemyID[i]].transform.childCount > 0)
        {
          Destroy(EnemyPoint[currentEnemyID[i]].GetChild(0).gameObject);
        }
      }
      currentEnemyID.Clear();
    }

    //根据初始点，类型继续游戏
    public void ActiveEnemyByType(Save save)
    {
      StopAllCoroutines();
      DeleteEnemy();
      currentPointEnemy.Clear();
      currentEnemyID = save.EnemyPoint;
      currentPointEnemy = save.Enemytype;
      ShootScoreManager.insernt.score.text = save.score;
      ShootScoreManager.insernt.shoot.text = save.shootNum;
      
      for (int i = 0; i < currentEnemyID.Count; i++)
      {
        int EnemyID = currentPointEnemy[i];
        GameObject obj=  GameObject.Instantiate(Enemys[EnemyID], EnemyPoint[currentEnemyID[i]].position+
                                                                 new Vector3(0,pointMove[EnemyID],0),
          Quaternion.identity);
        obj.transform.parent = EnemyPoint[currentEnemyID[i]];

      }

      StartCoroutine(QIYong());
    }
    //返回保存对象的内容
    public Save GetSaveData()
    {
      Save save = new Save();
      save.EnemyPoint = currentEnemyID;
      save.Enemytype = currentPointEnemy;
      save.score = ShootScoreManager.insernt.score.text;
      save.shootNum = ShootScoreManager.insernt.shoot.text;
      return save;
    }
  }

}

